//
//  GeneralNavSearchView.m
//  MobileShoppingMall
//
//  Created by 冯传昌 on 2021/4/16.
//  Copyright © 2021 冯传昌. All rights reserved.
//

#import "GeneralNavSearchView.h"

@interface GeneralNavSearchView()<UITextFieldDelegate>

@property (nonatomic,strong) UITextField *search_TF;

@end

@implementation GeneralNavSearchView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self initUI];
    }
    return self;
}

#pragma mark - setter
- (void)setSearchStr:(NSString *)searchStr{
    _searchStr = searchStr;
    self.search_TF.text = _searchStr;
}

#pragma mark - UI
-(void)initUI{
    
    UIImageView *imgV = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, kHeight_NavBar)];
    imgV.image = LoadImage(@"通用_导航背景");
    [self addSubview:imgV];

    UITextField *tf = [[UITextField alloc] initWithFrame:CGRectMake(10, kHeight_StatusBar + 5, ScreenWidth-80, 30)];
    tf.clipsToBounds = YES;
    tf.layer.cornerRadius = 15;
    tf.backgroundColor = RGBA(255, 255, 255, 0.8f);
    tf.placeholder = @"  请输入搜索内容";
    tf.leftView = [GeneralNavSearchView leftView];
    tf.leftViewMode = UITextFieldViewModeAlways;
    tf.returnKeyType = UIReturnKeySearch;
    tf.delegate = self;
    [self addSubview:tf];
    self.search_TF = tf;
    
    UIButton *menuBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    menuBtn.frame = CGRectMake(ScreenWidth-54, tf.top, 30, 30);
    [menuBtn setImage:LoadImage(@"首页_消息") forState:normal];
    [menuBtn setImageEdgeInsets:UIEdgeInsetsMake(5, 5, 5, 5)];
    [menuBtn addTarget:self action:@selector(showMenuClick) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:menuBtn];
}

+(UIView *)leftView{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 40, 30)];
    UIImageView *searchImgV = [[UIImageView alloc]initWithFrame:CGRectMake(25, 8, 15, 15)];
    searchImgV.image = LoadImage(@"首页_搜索");
    [view addSubview:searchImgV];
    return view;
}
//菜单
-(void)showMenuClick{
    if (self.showMenuBlock) {
        self.showMenuBlock();
    }
}

#pragma mark - UITextfiledDelegate
- (void)textFieldDidEndEditing:(UITextField *)textField{
    if (textField.text.length <= 0) {
        if (self.searchBlock) {
            self.searchBlock(textField.text);
        }
    }
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    if (self.searchBlock) {
        self.searchBlock(textField.text);
    }
    return YES;
}


@end


